<CFQUERY NAME="qryCINLEAVEAPPSM" DATASOURCE="#client.company_dsn#">
	SELECT *
	FROM CINLEAVEAPPSM
	WHERE PERSONNELIDNO = '#GETENTRY.PERSONNELIDNO#' AND DOCNUMBER = '#GETENTRY.DOCNUMBER#'
</CFQUERY>
<CFSET theDate= DATEFORMAT("#qryCINLEAVEAPPSM.STARTINGDATE#")>
<CFSET sNoofDays= DateDiff("d", "#qryCINLEAVEAPPSM.STARTINGDATE#", "#qryCINLEAVEAPPSM.ENDINGDATE#") + 1>
<CFOUTPUT QUERY="qryCINLEAVEAPPSM">
	<CFLOOP index="noofdays" FROM="1" TO="#sNoofDays#" STEP="1">
		<CFSET ValidateDate=DayOfWeekAsString(DayOfWeek("#theDate#"))>
		<CFIF ValidateDate NEQ "Sunday">
			<CFIF ValidateDate NEQ "Saturday">
				<CFIF qryCINLEAVEAPPSM.STAFFPID NEQ "">
					<CFQUERY NAME="insertCINLEAVEAPPSD" DATASOURCE="#client.company_dsn#">
						INSERT INTO CINLEAVEAPPSD(PERSONNELIDNO,REFERENCEDATE,LEAVEHOURS,LEAVETYPE,RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE,TIMELASTUPDATE,NOOFDAYS,DOCNUMBER,APPROVED,WITHPAY)
						VALUES('#STAFFPID#','#theDate#','#LEAVEHOURS#','#LEAVETYPE#','#RECCREATEDBY#','#RECDATECREATED#','#USERID#','#DATELASTUPDATE#','#TIMELASTUPDATE#','#NOOFDAYS#','#DOCNUMBER#','N','#WITHPAY#')
					</CFQUERY>
				<CFELSE>
					<CFQUERY NAME="insertCINLEAVEAPPSD" DATASOURCE="#client.company_dsn#">
						INSERT INTO CINLEAVEAPPSD(PERSONNELIDNO,REFERENCEDATE,LEAVEHOURS,LEAVETYPE,RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE,TIMELASTUPDATE,NOOFDAYS,DOCNUMBER,APPROVED,WITHPAY)
						VALUES('#PERSONNELIDNO#','#theDate#','#LEAVEHOURS#','#LEAVETYPE#','#RECCREATEDBY#','#RECDATECREATED#','#USERID#','#DATELASTUPDATE#','#TIMELASTUPDATE#','#NOOFDAYS#','#DOCNUMBER#','N','#WITHPAY#')
					</CFQUERY>
				</CFIF>
			</CFIF>
		</CFIF>
		<CFSET ValidateDate="">
		<CFSET theDate= DATEFORMAT(DateAdd("D",1, "#theDate#"))>
	</CFLOOP>
</CFOUTPUT>

